package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.mall.mapper.OrderItemMapper;
import com.lam.mall.entity.OrderItem;
import com.lam.mall.param.OrderItemParam;
import com.lam.mall.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 订单项Service业务层处理
 * 
 * @author admin
 * @date 2023-04-25
 */
@Service
public class OrderItemServiceImpl  implements OrderItemService {

    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 查询订单项
     * 
     * @param orderItemId ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public OrderItem selectById(String orderItemId) {

        return orderItemMapper.selectById(orderItemId);
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<OrderItem> selectByOrderNo(String orderNo) {
    
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number", orderNo);
        return orderItemMapper.selectList(queryWrapper);
    }
    
    @Override
	@Cacheable(cacheNames = "OrderItems", key = "#orderNo")
	public List<OrderItem> selectByOrderNoCache(String orderNo) {
    	
		return selectByOrderNo(orderNo);
	}

    /**
     * 查询订单项列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<OrderItem> selectList(OrderItemParam param) {
    
        QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(Objects.nonNull(param.getShopId()), "shop_id", param.getShopId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getOrderNumber()), "order_number", param.getOrderNumber());
        queryWrapper.eq(Objects.nonNull(param.getPayType()), "pay_type", param.getPayType());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDvyType()), "dvy_type", param.getDvyType());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDvyFlowNo()), "dvy_flow_no", param.getDvyFlowNo());
        queryWrapper.ge(Objects.nonNull(param.getCreateTimeBegin()), "create_time", param.getCreateTimeBegin());
        queryWrapper.le(Objects.nonNull(param.getCreateTimeEnd()), "create_time", param.getCreateTimeEnd());
        
        queryWrapper.eq(Objects.nonNull(param.getIsPayed()), "is_payed", param.getIsPayed());
        queryWrapper.eq(Objects.nonNull(param.getDeleteStatus()), "delete_status", param.getDeleteStatus());
        queryWrapper.eq(Objects.nonNull(param.getRefundSts()), "refund_sts", param.getRefundSts());
        queryWrapper.eq(Objects.nonNull(param.getOrderType()), "order_type", param.getOrderType());
        queryWrapper.eq(Objects.nonNull(param.getCloseType()), "close_type", param.getCloseType());
        
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getOrderNumber()), "order_number", param.getOrderNumber());
        queryWrapper.eq(Objects.nonNull(param.getProdId()), "prod_id", param.getProdId());

        return orderItemMapper.selectList(queryWrapper);
    }

    /**
     * 新增订单项
     * 
     * @param orderItem 
     * @return 
     */
    @Override
    @Transactional
    public int insert(OrderItem orderItem) {
    
        return orderItemMapper.insert(orderItem);
    }

    /**
     * 修改订单项
     * 
     * @param orderItem 
     * @return 
     */
    @Override
    @Transactional
    public int update(OrderItem orderItem) {
    
        return orderItemMapper.updateById(orderItem);
    }

    /**
     * 删除订单项信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return orderItemMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    
}
